package ssw.gui;

import java.util.Locale;
import java.util.TreeSet;

import javax.swing.JOptionPane;

import ssw.game.Game;
import ssw.game.GameResults;
import ssw.game.GameState;
import ssw.game.GameType;
import ssw.game.engine.GameEngine;

public class Simple {
	private final InputPanel inputPanel = new InputPanel();
	
	public static void main(String[] args) {
		System.out.println("Begin");
		
		Simple s = new Simple();
		s.go();
		
		System.out.println("End");
	}

	public void go()  {
		GameType type = GameType.TWO_LETTER_WORDS;
		GameEngine engine = Game.create(type);
		GameState state = engine.newGame();
		
		TreeSet<String> guesses = new TreeSet<String>();
		
		while (!state.isDone())  {
			inputPanel.setup(state, guesses);
			String guess = JOptionPane.showInputDialog(null, inputPanel, "Small Scrabble Words (" + state.getRemainingWords().size() + " words left)", JOptionPane.PLAIN_MESSAGE);
			if (guess == null)  {
				break;
			}
			else if ("".equals(guess))  {
				GameResults gr = GameResults.of(state, guesses);
				JOptionPane.showMessageDialog(null, gr.toString());
				
				state = engine.submit(state, guesses);
				
				guesses.clear();
			}
			else  {
				final String g = guess.trim().toUpperCase(Locale.CANADA);
				boolean present = guesses.contains(g);
				if (present)  {
					guesses.remove(g);
				}
				else  {
					guesses.add(g);
				}
			}
		}
	}
}
